#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;

namespace UTable.Objects.Controls
{
    public class ControlInfoTree: IVisualTree
    {
        private ControlInfo _root;

        public ControlInfoTree(ControlInfo root)
        {
            _root = root;
        }

        #region IVisualTree Members

        public IVisualNode TreeRoot
        {
            get
            {
                return _root;
            }
        }

        public double TreeWidth
        {
            get
            {
                if (_root == null)
                    return 0;
                else
                    return _root.Width;
            }
        }

        public double TreeHeight
        {
            get
            {
                if (_root == null)
                    return 0;
                else
                    return _root.Height;
            }
        }

        public IVisualNode GetNodeByID(string id)
        {
            return GetNodeByID(id, _root);
        }

        public IVisualNode GetNodeByID(String id, ControlInfo node)
        {
            if (node.ID == id)
                return node;
            foreach (ControlInfo info in node.Children)
            {
                IVisualNode n = GetNodeByID(id, info);
                if (n != null)
                    return n;
            }
            return null;
        }

        public IVisualNode GetParent(IVisualNode node)
        {
            if (node is ControlInfo)
                return (node as ControlInfo).Parent;
            else
                return null;
        }

        public IVisualNode GetChild(IVisualNode node, int index)
        {
            if (node is ControlInfo && (node as ControlInfo).Children.Count > index)
                return (node as ControlInfo).Children[index];
            else
                return null;
        }

        public int GetChildrenCount(IVisualNode node)
        {
            if (node is ControlInfo)
                return (node as ControlInfo).Children.Count;
            else
                return 0;
        }

        public HitTestResult HitTest(HitTestParameters hitTestParameters)
        {
            return (this.TreeRoot as ControlInfo).HitTest(hitTestParameters);
        }

        public Point TranslatePoint(Point point, IVisualNode relativeTo)
        {
            return (TreeRoot as ControlInfo).TranslatePoint(point, relativeTo);
        }

        public Point TranslatePoint(IVisualNode node, Point point, IVisualNode relativeTo)
        {
            if (!(node is ControlInfo))
            {
                return new Point(double.NaN, double.NaN);
            }
            return (node as ControlInfo).TranslatePoint(point, relativeTo);
        }

        #endregion
    }
}
